home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-config-printer / ToolbarSearchEntry.py < prev    next >
Encoding:
Python Source  |  2009-05-05  |  6.2 KB  |  199 lines

  1. ## This code was translated to python from the original C version in
  2. ## Rhythmbox. The original authors are:
  3.  
  4. ## Copyright (C) 2002 Jorn Baayen <jorn@nl.linux.org>
  5. ## Copyright (C) 2003 Colin Walters <walters@verbum.org>
  6.  
  7. ## Further modifications by:
  8.  
  9. ## Copyright (C) 2008 Rui Matos <tiagomatos@gmail.com>
  10.  
  11. ## This program is free software; you can redistribute it and/or modify
  12. ## it under the terms of the GNU General Public License as published by
  13. ## the Free Software Foundation; either version 2 of the License, or
  14. ## (at your option) any later version.
  15.  
  16. ## This program is distributed in the hope that it will be useful,
  17. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ## GNU General Public License for more details.
  20.  
  21. ## You should have received a copy of the GNU General Public License
  22. ## along with this program; if not, write to the Free Software
  23. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. import gobject
  26. import gtk
  27. try:
  28.     import sexy
  29.     has_sexy = True
  30. except:
  31.     has_sexy = False
  32. import HIG
  33. from gettext import gettext as _
  34.  
  35. class ToolbarSearchEntry (gtk.HBox):
  36.     __gproperties__ = {
  37.         'search_timeout' : (gobject.TYPE_UINT,
  38.                             'search timeout',
  39.                             'search signal rate limiter (in ms)',
  40.                             0,
  41.                             5000,
  42.                             300,
  43.                             gobject.PARAM_READWRITE)
  44.         }
  45.  
  46.     __gsignals__ = {
  47.         'search' : (gobject.SIGNAL_RUN_LAST,
  48.                     gobject.TYPE_NONE,
  49.                     [ gobject.TYPE_STRING ]),
  50.         'activate' : (gobject.SIGNAL_RUN_LAST,
  51.                       gobject.TYPE_NONE,
  52.                       [])
  53.         }
  54.  
  55.     def __init__ (self):
  56.         global has_sexy
  57.         self.sexy = has_sexy
  58.         self.entry = None
  59.         self.timeout = 0
  60.         self.is_a11y_theme = False
  61.         self.search_timeout = 300
  62.         self.menu = None
  63.  
  64.         gtk.HBox.__gobject_init__ (self)
  65.         self.set_spacing (HIG.PAD_NORMAL)
  66.         self.set_border_width (HIG.PAD_NORMAL)
  67.  
  68.         settings = gtk.settings_get_for_screen (self.get_screen ())
  69.         theme = settings.get_property ('gtk-theme-name')
  70.         self.is_a11y_theme = theme == 'HighContrast' or theme == 'LowContrast'
  71.  
  72.         label = gtk.Label ()
  73.         label.set_text_with_mnemonic (_("_Filter:"))
  74.         label.set_justify (gtk.JUSTIFY_RIGHT)
  75.         self.pack_start (label, False, True, 0);
  76.  
  77.         if self.sexy:
  78.             self.entry = sexy.IconEntry ()
  79.             self.entry.add_clear_button ()
  80.             self.entry.set_icon (sexy.ICON_ENTRY_PRIMARY,
  81.                                  gtk.image_new_from_stock (gtk.STOCK_FIND,
  82.                                                            gtk.ICON_SIZE_MENU))
  83.         else:
  84.             self.entry = gtk.Entry()
  85.  
  86.         label.set_mnemonic_widget (self.entry)
  87.  
  88.         self.pack_start (self.entry, True, True, 0)
  89.  
  90.         self.entry.connect ('changed', self.on_changed)
  91.         self.entry.connect ('focus_out_event', self.on_focus_out_event)
  92.         self.entry.connect ('activate', self.on_activate)
  93.         if self.sexy:
  94.             self.entry.connect ("icon-pressed", self.on_icon_pressed)
  95.  
  96.     def do_get_property (self, property):
  97.         if property.name == 'search_timeout':
  98.             return self.search_timeout
  99.         else:
  100.             raise AttributeError, 'unknown property %s' % property.name
  101.  
  102.     def do_set_property (self, property, value):
  103.         if property.name == 'search_timeout':
  104.             self.search_timeout = value
  105.         else:
  106.             raise AttributeError, 'unknown property %s' % property.name
  107.  
  108.     def clear (self):
  109.         if self.timeout != 0:
  110.             gobject.source_remove (self.timeout)
  111.             self.timeout = 0
  112.  
  113.         self.entry.set_text ("")
  114.  
  115.     def get_text (self):
  116.         return self.entry.get_text ()
  117.  
  118.     def set_text (self, text):
  119.         self.entry.set_text (text)
  120.  
  121.     def check_style (self):
  122.         if self.is_a11y_theme:
  123.             return
  124.  
  125.         bg_colour = gtk.gdk.color_parse ('#f7f7be') # yellow-ish
  126.         fg_colour = gtk.gdk.color_parse ('#000000') # black
  127.  
  128.         text = self.entry.get_text ()
  129.         if len (text) > 0:
  130.             self.entry.modify_text (gtk.STATE_NORMAL, fg_colour)
  131.             self.entry.modify_base (gtk.STATE_NORMAL, bg_colour)
  132.         else:
  133.             self.entry.modify_text (gtk.STATE_NORMAL, None)
  134.             self.entry.modify_base (gtk.STATE_NORMAL, None)
  135.  
  136.         self.queue_draw ()
  137.  
  138.     def on_changed (self, UNUSED):
  139.         self.check_style ()
  140.  
  141.         if self.timeout != 0:
  142.             gobject.source_remove (self.timeout)
  143.             self.timeout = 0
  144.  
  145.            # emit it now if we have no more text
  146.         if len (self.entry.get_text ()) > 0:
  147.             self.timeout = gobject.timeout_add (self.search_timeout,
  148.                                                 self.on_search_timeout)
  149.         else:
  150.             self.on_search_timeout ()
  151.  
  152.     def on_search_timeout (self):
  153.         self.emit ('search', self.entry.get_text ())
  154.         self.timeout = 0
  155.  
  156.         return False
  157.  
  158.     def on_focus_out_event (self, UNUSED_widget, UNUSED_event):
  159.         if self.timeout == 0:
  160.             return False
  161.  
  162.         gobject.source_remove (self.timeout)
  163.         self.timeout = 0
  164.  
  165.         self.emit ('search', self.entry.get_text ())
  166.  
  167.         return False
  168.  
  169.     def searching (self):
  170.         return self.entry.get_text () != ''
  171.  
  172.     def on_activate (self, UNUSED_entry):
  173.         self.emit ('search', self.entry.get_text ())
  174.  
  175.     def grab_focus (self):
  176.         self.entry.grab_focus ()
  177.  
  178.     def set_drop_down_menu (self, menu):
  179.         if not self.sexy:
  180.             return
  181.         if menu:
  182.             self.entry.set_icon_highlight (sexy.ICON_ENTRY_PRIMARY, True)
  183.             self.menu = menu
  184.         else:
  185.             self.entry.set_icon_highlight (sexy.ICON_ENTRY_PRIMARY, False)
  186.             self.menu = None
  187.  
  188.     def on_icon_pressed (self, UNUSED, icon_position, button):
  189.         if not self.sexy:
  190.             return
  191.         if icon_position != sexy.ICON_ENTRY_PRIMARY:
  192.             return
  193.         if not self.menu:
  194.             return
  195.  
  196.         self.menu.popup (None, None, None, button, 0L)
  197.  
  198. gobject.type_register (ToolbarSearchEntry)
  199.